package com.intelligent.system.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.system.common.dto.TagCreateAndAssociateDTO;
import com.intelligent.system.common.dto.TagDTO;
import com.intelligent.system.common.dto.TagPageDTO;
import com.intelligent.system.common.response.ResultDO;
import com.intelligent.system.common.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 标签控制器
 */
@Api(tags = "标签管理接口")
@RestController
@RequestMapping("/api/tags")
public class TagController {

    @Autowired
    private TagService tagService;

    @ApiOperation("根据ID查询标签")
    @GetMapping("/{id}")
    public ResultDO<TagDTO> getById(
            @ApiParam(value = "标签ID", required = true)
            @PathVariable Long id) {
        return tagService.getById(id);
    }


    @ApiOperation("新增标签")
    @PostMapping
    public ResultDO<Void> saveTag(
            @ApiParam(value = "标签信息", required = true)
            @Validated @RequestBody TagDTO tagDTO) {
        return tagService.saveTag(tagDTO);
    }

    @ApiOperation("更新标签")
    @PutMapping("/{id}")
    public ResultDO<Void> updateTag(
            @ApiParam(value = "标签ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "标签信息", required = true)
            @Validated @RequestBody TagDTO tagDTO) {
        tagDTO.setId(id);
        return tagService.updateTag(tagDTO);
    }

    @ApiOperation("删除标签")
    @GetMapping("/delete/{id}")
    public ResultDO<Void> removeById(
            @ApiParam(value = "标签ID", required = true)
            @PathVariable Long id) {
        return tagService.removeById(id);
    }

    @ApiOperation("分页查询标签列表")
    @PostMapping("/page")
    public ResultDO<IPage<TagDTO>> page(
            @ApiParam(value = "分页查询参数", required = true)
            @Validated @RequestBody TagPageDTO pageDTO) {
        return tagService.page(pageDTO);
    }

    @ApiOperation("创建标签并关联对象")
    @PostMapping("/create-and-associate")
    public ResultDO<?> createAndAssociate(
            @ApiParam(value = "创建并关联参数", required = true)
            @Validated @RequestBody TagCreateAndAssociateDTO dto) {
       return tagService.createAndAssociate(dto);
    }

    @ApiOperation("根据对象ID和对象类型查询标签列表")
    @GetMapping("/object/{objectId}/{objectType}")
    public ResultDO<List<TagDTO>> listByObjectId(
            @ApiParam(value = "对象ID", required = true)
            @PathVariable Long objectId,
            @ApiParam(value = "对象类型", required = true)
            @PathVariable Integer objectType) {
        return tagService.listByObjectId(objectId, objectType);
    }
} 